var dengluBox1 = document.getElementById("dengluBox1")
var loginBox = document.getElementById("loginBox")
const service = "/api/Questionnaire/v1/"

function alertMsg(){ // 错误弹框关闭
	var errorBox = document.getElementById('errorBox')
	errorBox.style.display = "none"
}
function yidenglu(){ // 右上角登录标志
	if (dengluBox1.style.display == 'block'){
		dengluBox1.style.display = 'none'
	} else {
		dengluBox1.style.display = "block"
	}
}

if((navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i))) {
	window.location.href="http://m.woniujiashi.com";
}else{
	// window.location.href="http://www.woniujiashi.com";
}


//获取用户基本信息
function getUserInfo(){
	$.ajax({
		type: "get",
		url: "/api/jiali_api/v1/user/select",
		beforeSend: function(xhr) {
			xhr.setRequestHeader("Authorization", "Bearer "+$.cookie("token"));
		},
		success: function(res){

				$.cookie("userInfo", JSON.stringify(res), { expires:604800});
				userInfo();
		}
	});
}
//判断用户登录状态
function userInfo(){
	if($.cookie('userInfo')){
		console.log($.cookie('userInfo'))
		if(JSON.parse($.cookie('userInfo')).name){
			var pattern1 = /^[\u4e00-\u9fa5]{1}/g;
			var name = JSON.parse($.cookie('userInfo')).name.match(pattern1)[0]+"**"
			$('#userInfoName').html(name)
			localStorage.setItem('name', name)
		}else{
			var pattern1 = /^(\d{3})\d{4}(\d{4})$/;
			// console.log(JSON.parse($.cookie('userInfo')));
			var phone = JSON.parse($.cookie('userInfo')).phone.replace(pattern1, '$1****$2')
			
			$('#userInfoName').html(phone)
			localStorage.setItem('phone', phone)
			
		}
		document.getElementsByClassName('yidenglu')[0].style.display = 'block'
		document.getElementsByClassName('loginBox')[0].style.display = 'none'
	}
}
userInfo()
function liveOut(){ // 退出登录
	var yidenglu = document.getElementById("yidenglu")
	yidenglu.style.display = 'none'
	loginBox.style.display = 'flex'
	$.cookie("userInfo", "",{expires:-1});
	$.cookie("toekn", "",{expires:-1});
	localStorage.removeItem('token')
	localStorage.removeItem('name')
	localStorage.removeItem('phone')
	userInfo()
}
var zhanghaologin = document.getElementById('zhanghaologin')
var fastlogin = document.getElementById('fastlogin')
var denglubox = document.getElementById('denglubox')
var zhucebox = document.getElementById('zhucebox')
function dengluTabAc(){// 快捷登录
	zhanghaologin.style.display="block";
	fastlogin.style.display="none";
	document.getElementsByClassName('fastbg')[0].style.backgroundColor = "#fff";
	document.getElementsByClassName('zhanghaobg')[0].style.backgroundColor = "#eceff4";
}
function loginBt(){// 点击快捷登录的登录按钮
	var fastlogin = document.forms["loginForm"]["phone"].value;
	var fastpassword = document.forms["loginForm"]["password"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastlogin))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else if(fastpassword =="" || fastpassword.length<5){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "密码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else{
		var data = {'phone':fastlogin,'password':fastpassword};
		$.post(service+"front_user/front_login",data,function(res){
				if(res.status_code=="200"){
					document.getElementsByClassName('loginBox')[0].style.display = 'none'
					document.getElementsByClassName('yidenglu')[0].style.display = 'block'
					denglubox.style.display="none";
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					
					localStorage.setItem('isLogin', true)
					getUserInfo();   
				}else{
					var errorBox = document.getElementById('errorBox')
					document.getElementById('errorBox').innerHTML = res.message
					errorBox.style.display = "block"
					window.setTimeout("alertMsg()",2000)
				}
        });
	}
}
var TIME_COUNTregist = 60 
var registFormtimer = null // 注册倒计时
var registFormcount = '' // 注册倒计时60秒
function getregistYan(){ // 注册弹框发送验证码
	var registlogin = document.forms["registForm"]["phone"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else{
		data = {'phone':registlogin,'type':1}
		$.post(service+"front_user/phone_code",data,function(res){
			if(res.status_code!="200"){
				var errorBox = document.getElementById('errorBox')
				document.getElementById('errorBox').innerHTML = res.message
				errorBox.style.display = "block"
				window.setTimeout("alertMsg()",2000)
				return
			}else{
				document.getElementById('registweifasong').style.display = 'none'
				document.getElementById('registyifasong').style.display = 'block'
				if (!this.registFormtimer) {
					this.registFormcount = TIME_COUNTregist;
					// this.registFormregistYan = false;
					this.registFormtimer = setInterval(() => {
					if (this.registFormcount > 0 && this.registFormcount <= TIME_COUNTregist) {
						this.registFormcount--;
						document.getElementById('registNum').innerText = this.registFormcount
						} else {
						document.getElementById('registweifasong').style.display = 'block'
						document.getElementById('registyifasong').style.display = 'none'
						clearInterval(this.registFormtimer);
						this.registFormtimer = null;
						}
					}, 1000)
				}
			}
		})
		
	}
	
}
var ischecked = false
function changeSelectAc(){ // 判断是否阅读协议
	var uncheckedFlag = document.getElementById('unchecked')
	var checkedFlag = document.getElementById('checked')
	if(checkedFlag.style.display == "none"){
		this.ischecked = true
		checkedFlag.style.display = "block"
		uncheckedFlag.style.display = "none"
	} else {
		this.ischecked = false
		checkedFlag.style.display = "none"
		uncheckedFlag.style.display = "block"
	}
}
function registAction(){ // 注册确认按钮
	var registlogin = document.forms["registForm"]["phone"].value;
	var registpassword = document.forms["registForm"]["password"].value;
	var registcode = document.forms["registForm"]["code"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else if(registpassword==null || registpassword.length<6){
		var errorBox = document.getElementById('errorBox')
		// document.getElementById('errorBox').innerHTML = "手机密码长度至少为6位数号不正确"
		document.getElementById('errorBox').innerHTML = "密码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else if (!(/^\d{6}$/.test(registcode))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "验证码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else if(!ischecked){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "请先阅读并同意《蜗牛家事用户注册和使用协议》"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else {

		var data = {'phone':registlogin,'password':registpassword,'code':registcode};
		$.post(service+"front_user/add_user",data,function(res){
				console.log(res)
				if(res.status_code=="200"){
					document.getElementsByClassName('unregist')[0].style.display = 'none'
					document.getElementsByClassName('registsuccess')[0].style.display = 'block'
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					localStorage.setItem('isLogin', true)
					getUserInfo();
					userInfo();
				}else{
					var errorBox = document.getElementById('errorBox')
					document.getElementById('errorBox').innerHTML = res.message
					errorBox.style.display = "block"
					window.setTimeout("alertMsg()",2000)
				}
		});

	}
}
function zhanghaoAc(){// 密码登录
	zhanghaologin.style.display="none";
	fastlogin.style.display="block";
	document.getElementsByClassName('fastbg')[0].style.backgroundColor = "#eceff4";
	document.getElementsByClassName('zhanghaobg')[0].style.backgroundColor = "#fff";
}
var TIME_COUNT = 60 
var loginFormtimer = null // 快捷登录倒计时
var loginFormcount = '' // 快捷登录倒计时60秒
function loginFormgetYan(){ // 登录弹框发送验证码
	var fastlogin = document.forms["loginForm"]["zhanghao"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastlogin))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else {
	
	data = {'phone':fastlogin,'type':5}
	$.post(service+"front_user/phone_code",data,function(res){
		if(res.status_code!="200"){
			var errorBox = document.getElementById('errorBox')
				document.getElementById('errorBox').innerHTML = res.message
				errorBox.style.display = "block"
				window.setTimeout("alertMsg()",2000)
				return 
		}else{
			document.getElementById('loginweifasong').style.display = 'none'
	document.getElementById('loginyifasong').style.display = 'block'
			if (!this.loginFormtimer) {
				this.loginFormcount = TIME_COUNT;
				// this.loginFormregistYan = false;
				this.loginFormtimer = setInterval(() => {
					if (this.loginFormcount > 0 && this.loginFormcount <= TIME_COUNT) {
						this.loginFormcount--;
						document.getElementById('loginNum').innerText = this.loginFormcount
					} else {
						document.getElementById('loginweifasong').style.display = 'block'
						document.getElementById('loginyifasong').style.display = 'none'
						clearInterval(this.loginFormtimer);
						this.loginFormtimer = null;
					}
				}, 1000)
				}
		}
	})
	
	}
}
function goRegistAction() {// 注册账号
	denglubox.style.display="none";
	zhucebox.style.display="block";
}
// 账号登录按钮
function fastloginBt(){
	var fastzhanghao = document.forms["loginForm"]["zhanghao"].value;
	var fastcode = document.forms["loginForm"]["code"].value;
	if (!(/^1[3456789]\d{9}$/.test(fastzhanghao))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else if(fastcode=="" || fastcode.length<6){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "验证码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else{
		var data = {'phone':fastzhanghao,'code':fastcode};
		$.post(service+"front_user/code_login_phone",data,function(res){
				console.log(res)
				if(res.status_code=="200"){
					document.getElementsByClassName('loginBox')[0].style.display = 'none'
					document.getElementsByClassName('yidenglu')[0].style.display = 'block'
					denglubox.style.display="none";
					$.cookie("token", res.data.token, { expires: res.data.expires_in});
					localStorage.setItem('topins',0)
					localStorage.setItem('token', res.data.token) // 存储token
					localStorage.setItem('isLogin', true)
					getUserInfo();
				}else{
					var errorBox = document.getElementById('errorBox')
					document.getElementById('errorBox').innerHTML = res.message
					errorBox.style.display = "block"
					window.setTimeout("alertMsg()",2000)
				}
        });
	}
}
function closeLoginDialog(){  // 关闭登录弹窗
	denglubox.style.display="none";
}
function loginAc(){ // 打开登录弹窗
	var denglubox = document.getElementById('denglubox')
	denglubox.style.display="block";
	document.forms["loginForm"]["phone"].value = ''
	document.forms["loginForm"]["zhanghao"].value = ''
	document.forms["loginForm"]["password"].value = ''
	document.forms["loginForm"]["code"].value = ''
}
function registAc() { // 打开注册弹窗
	zhucebox.style.display="block";
	document.forms["registForm"]["phone"].value = ''
	document.forms["registForm"]["password"].value = ''
	document.forms["registForm"]["code"].value = ''
}
function tishibox(){ // 点击提示框
	var yuanyin = document.getElementById("yuanyin")
	if(yuanyin.style.display == "none"){
		yuanyin.style.display = "block"
	} else {
		yuanyin.style.display = "none"
	}
}
function xiufuzhanghao(){//修复账号
	var xiufuBox = document.getElementById("xiufuBox")
	if(xiufuBox.style.display == "none"){
		xiufuBox.style.display = "block"
	} else {
		xiufuBox.style.display = "none"
	}
}
function closeforgetDialog2(){ // 关闭重置密码弹窗2
	resetpasswordBox2.style.display = 'none'
}
function ungetcodeAction(){// 重置密码弹窗2收不到验证码
	var ungetcode = document.getElementById("ungetcode")
	if(ungetcode.style.display == "none"){
		ungetcode.style.display = "block"
	} else {
		ungetcode.style.display = "none"
	}
}
function closeDialog() { // 关闭注册弹窗
	zhucebox.style.display="none";
}
function goregist(){ // 注册
	denglubox.style.display="none";
	zhucebox.style.display="block";
}
function goLoginAction(){ 
	denglubox.style.display="block";
	zhucebox.style.display="none";
}
var resetpasswordBox1 = document.getElementById("resetpasswordBox1")
var resetpasswordBox2 = document.getElementById("resetpasswordBox2")
function forgetAc(){ // 点击忘记密码
	resetpasswordBox1.style.display = 'block'
	denglubox.style.display="none";
	document.forms["resetpasswordForm"]["phone"].value = ''
}
function forgetNext(){ // 重置密码弹窗1下一步
	var registlogin = document.forms["resetpasswordForm"]["phone"].value;
	if (!(/^1[3456789]\d{9}$/.test(registlogin))){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "手机号不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else {
		document.forms["resetpasswordForm2"]["code"].value = ''
		resetpasswordBox1.style.display = 'none'
		resetpasswordBox2.style.display = 'block'
		document.getElementById('resetpasswordBox2Num').innerText = registlogin
	}
	
}
function closeforgetDialog(){// 关闭重置密码弹窗
	resetpasswordBox1.style.display = 'none'
}
var resetpasswordFormtimer = null // 快捷登录倒计时
var resetpasswordFormcount = '' // 快捷登录倒计时60秒
function findYan(){ // 重置密码弹框2获取验证码
	document.getElementById('resetpasswordweiyanzheng').style.display = 'none'
	document.getElementById('resetpasswordyiyanzheng').style.display = 'block'
	var registlogin = document.forms["resetpasswordForm"]["phone"].value;
	data = {'phone':registlogin,'type':3}
	$.post(service+"front_user/phone_code",data,function(res){
		if(res.status_code!="200"){
			return
		}
	})
	if (!this.resetpasswordFormtimer) {
	  this.resetpasswordFormcount = TIME_COUNT;
	  // this.loginFormregistYan = false;
	  this.resetpasswordFormtimer = setInterval(() => {
	  if (this.resetpasswordFormcount > 0 && this.resetpasswordFormcount <= TIME_COUNT) {
			 this.resetpasswordFormcount--;
			 document.getElementById('resetpasswordNum').innerText = this.resetpasswordFormcount
			} else {
			 document.getElementById('resetpasswordweiyanzheng').style.display = 'block'
			 document.getElementById('resetpasswordyiyanzheng').style.display = 'none'
			 clearInterval(this.resetpasswordFormtimer);
			 this.resetpasswordFormtimer = null;
			}
	  }, 1000)
	 }
}
function resetPrev(){ // 忘记密码弹窗2上一步
	resetpasswordBox1.style.display = 'block'
	resetpasswordBox2.style.display = 'none'
}
function resetnext(){// 忘记密码弹窗2下一步
	var registcode = document.forms["resetpasswordForm2"]["code"].value;
	if(registcode=='' || registcode.length<6){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "验证码不正确"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else {
		var registlogin = document.forms["resetpasswordForm"]["phone"].value;
		data = {'phone':registlogin,'type':3,'code':registcode}
		$.post(service+"front_user/phone_code_v",data,function(res){
			if(res.status_code!="200"){
				var errorBox = document.getElementById('errorBox')
				document.getElementById('errorBox').innerHTML = res.message
				errorBox.style.display = "block"
				window.setTimeout("alertMsg()",2000)
				return 
			}else{
				resetpasswordBox2.style.display = 'none'
		document.forms["resetpasswordForm3"]["password"].value = "";
		document.forms["resetpasswordForm3"]["passwordAgain"].value = ""
		resetpasswordBox3.style.display = 'block'
			}
		})
		

	}
}
function closePhonePw() { // 关闭忘记密码弹窗3
	resetpasswordBox3.style.display = 'none'
}
function resetPWprev() { // 忘记密码弹窗3上一步
	resetpasswordBox3.style.display = 'none'
	resetpasswordBox2.style.display = 'block'
}
function resetPW(){ // 忘记密码弹窗3完成
	var resetpassword = document.forms["resetpasswordForm3"]["password"].value;
	var resetpasswordagain = document.forms["resetpasswordForm3"]["passwordAgain"].value;
	if(resetpassword =='' || resetpasswordagain ==''){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "密码不能为空"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else  if(resetpassword !==resetpasswordagain){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "两次密码输入不一致"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}else if (resetpassword.length<6 || resetpasswordagain.length<6){
		var errorBox = document.getElementById('errorBox')
		document.getElementById('errorBox').innerHTML = "密码不能少于6位数"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	} else {
		var registlogin = document.forms["resetpasswordForm"]["phone"].value;
		data = {'phone':registlogin,'password':resetpassword,'passwordagain':resetpasswordagain}
		$.post(service+"front_user/update_password_phone",data,function(res){
			if(res.status_code!="200"){
				var errorBox = document.getElementById('errorBox')
				document.getElementById('errorBox').innerHTML = res.message
				errorBox.style.display = "block"
				window.setTimeout("alertMsg()",2000)
			}
		})
		resetpasswordBox3.style.display = 'none'
		document.getElementById('errorBox').innerHTML = "密码重置成功"
		errorBox.style.display = "block"
		window.setTimeout("alertMsg()",2000)
	}
}

// function getQueryVariable(variable)
// {
// 	var query = window.location.search.substring(1);
// 	var vars = query.split("&");
// 	for (var i=0;i<vars.length;i++) {
// 			var pair = vars[i].split("=");
// 			if(pair[0] == variable){return pair[1];}
// 	}
// 	return(false);
// }
function navRouter(e){
	if (e==3){
	 localStorage.setItem('topins',2)
	 var isLogin = localStorage.getItem('token')
	 if (isLogin == undefined){
	  var denglubox = document.getElementById('denglubox')
	  denglubox.style.display = 'block'
	 } else {
	  window.location.href = "/dzlhxys"
	 }
	} 
}
function searchAction(){
	var title = $("#select_news").val();
	// alert(title);
	window.location.href="/lhzs/select_os_blade_news?title="+title;
}

// function page_no(){
// 	var lis = $(".el-pager li");
// 	// console.log(lis[0])
// 	if(lis.length==0){
// 		// $("#errormin").style.display = "block"
// 		$(".el-pager").append("<li class='active'><span>1</span></li>")
// 	} else {
// 		// $("#errormin").style.display = "none"
// 	}
// }
// page_no()

// function select_list(){
	
// 	var lis = $(".min_l li");
// 	if(lis.length==0){
// 		document.getElementById("errormin").style.display = "block"
// 		// $(".el-pager").append("<li class='active'><span>1</span></li>")
// 	} else {
// 		document.getElementById("errormin").style.display = "none"
// 	}
// }
// select_list()


$(document).mouseup(function(e) {
	if($(e.target).closest("#dialogLogin").length == 0){  // Mark 1
	 document.getElementById("denglubox").style.display = "none"
	}
	if($(e.target).closest("#dialogFormVisible").length == 0){  // Mark 1
	 document.getElementById('zhucebox').style.display = "none"
	}
	if($(e.target).closest("#forgetDialog").length == 0){  // Mark 1
	 document.getElementById('resetpasswordBox1').style.display = "none"
	}
	if($(e.target).closest("#forgetDialog2").length == 0){  // Mark 1
	document.getElementById('resetpasswordBox2').style.display = "none"
	}
	if($(e.target).closest("#forgetDialog3").length == 0){  // Mark 1
	 document.getElementById('resetpasswordBox3').style.display = "none"
	}
  });